﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ProcessoSeletivo.DAO
{
    public class InscricaoCandidato : IDisposable
    {
        #region " Listar "

        public DataView ListarInscricao()
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ListarInscricao", sql);
                sqlc.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_inscricao");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        #endregion

        #region " Consultar "

        public DataView ConsultarInscricaoCompleta(int cod_inscricao_ins)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ConsultarInscricaoCompleta", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_inscricao_ins", cod_inscricao_ins);

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_inscricao");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        public DataView ConsultarInscricao(int cod_inscricao_ins)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ConsultarInscricao", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_inscricao_ins", cod_inscricao_ins);

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_inscricao");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }

        public DataView ConsultarInscricaoPorCandidato(int cod_candidato_can)
        {
            DataSet lRetorno = new DataSet();
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ConsultarInscricaoPorCandidato", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_candidato_can", cod_candidato_can);

                SqlDataAdapter sqa = new SqlDataAdapter(sqlc);
                sqa.Fill(lRetorno, "tb_pse_inscricao");
                sql.Close();
            }
            return lRetorno.Tables[0].DefaultView;
        }
        
        #endregion

        #region " Incluir "

        public int IncluirInscricao(DataSet dsRegistro)
        {
            int PrimaryKey = 0;

            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("IncluirInscricao", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_sala_sal", DBNull.Value);
                sqlc.Parameters.AddWithValue("@cod_local_prova_lpr", DBNull.Value);
                sqlc.Parameters.AddWithValue("@flg_isento_ins", dsRegistro.Tables[0].Rows[0]["flg_isento_ins"]);

                PrimaryKey = Convert.ToInt32(sqlc.ExecuteScalar().ToString());
                sql.Close();
            }

            return PrimaryKey;
        }

        #endregion

        #region " Alterar "

        public void AlterarInscricao(DataSet dsRegistro)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("AlterarInscricao", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_inscricao_ins", dsRegistro.Tables[0].Rows[0]["cod_inscricao_ins"]);
                sqlc.Parameters.AddWithValue("@cod_sala_sal", dsRegistro.Tables[0].Rows[0]["cod_sala_sal"]);
                sqlc.Parameters.AddWithValue("@cod_local_prova_lpr", dsRegistro.Tables[0].Rows[0]["cod_local_prova_lpr"]);
                sqlc.Parameters.AddWithValue("@flg_isento_ins", dsRegistro.Tables[0].Rows[0]["flg_isento_ins"]);
                sqlc.ExecuteNonQuery();

                sql.Close();
            }
        }

        #endregion

        #region " Excluir "

        public void ExcluirInscricaoPorCandidato(int cod_candidato_can)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ExcluirInscricaoPorCandidato", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_candidato_can", cod_candidato_can);
                sqlc.ExecuteNonQuery();

                sql.Close();
            }
        }

        public void ExcluirInscricao(int cod_inscricao_ins)
        {
            using (SqlConnection sql = new SqlConnection(ConfigurationManager.ConnectionStrings["StringConexao"].ToString()))
            {
                sql.Open();
                SqlCommand sqlc = new SqlCommand("ExcluirInscricao", sql);
                sqlc.CommandType = CommandType.StoredProcedure;

                sqlc.Parameters.AddWithValue("@cod_inscricao_ins", cod_inscricao_ins);
                sqlc.ExecuteNonQuery();

                sql.Close();
            }
        }

        #endregion

        #region " Dipose "

        public void Dispose()
        {
            GC.SuppressFinalize(this);
        }

        #endregion
    }
}
